package com.satan.elasticsearch.listener.mq;

import com.satan.elasticsearch.constant.HotelMqConsts;
import com.satan.elasticsearch.service.HotelEsService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: Demon
 * @Date: 2024/2/19 00:35
 * @Description:
 **/
@Component
@RequiredArgsConstructor
public class HotelListener {
    private final HotelEsService hotelEsService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConsts.HOTEL_INSERT_QUEUE),
            exchange = @Exchange(name = HotelMqConsts.HOTEL_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = HotelMqConsts.HOTEL_INSERT_KEY
    ))
    public void listenHotelInsert(Long id) throws IOException {
        // 新增
        hotelEsService.saveHotelById(id);
        System.out.println("监听到新增酒店：" + id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConsts.HOTEL_DELETE_QUEUE),
            exchange = @Exchange(name = HotelMqConsts.HOTEL_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = HotelMqConsts.HOTEL_DELETE_KEY
    ))
    public void listenHotelDelete(Long id) throws IOException {
        // 删除
        hotelEsService.deleteHotelById(id);
        System.out.println("监听到删除酒店：" + id);
    }
}
